package com.cuitrip.business.tripservice.proxy;

import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;

import com.cuitrip.business.bill.Rate;
import com.cuitrip.business.tripservice.model.SelectMenu;
import com.cuitrip.business.tripservice.model.ServiceInfo;
import com.cuitrip.business.tripservice.ui.SelectMenuFragment;
import com.cuitrip.service.R;
import com.cuitrip.util.ResourceUtil;
import com.lab.app.BaseActivity;
import com.lab.utils.NumberUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 价格逻辑代理类
 * Created by ak on 15/9/23.
 */
public class PriceProxy {


    private static PriceProxy sInstance;

    public static PriceProxy getInstance() {
        if (null == sInstance)
            sInstance = new PriceProxy();
        return sInstance;
    }

    public interface CurrencyCallback {

        public void onCurrencySelect(String currency);

    }


    public void showCurrencySelect(BaseActivity activity, List<Rate> rateList, final CurrencyCallback currencyCallback) {
        SelectMenuFragment selectMenuFragment = new SelectMenuFragment();
        List<SelectMenu> selectMenuList = new ArrayList<>();
        for (int i = 0; i < rateList.size(); i++) {
            SelectMenu selectMenu = new SelectMenu();
            Rate rate = rateList.get(i);
            selectMenu.key = rate.currency.toUpperCase();
            selectMenu.value = rate.currency.toUpperCase();
            selectMenuList.add(selectMenu);
        }
        selectMenuFragment.setTitle(ResourceUtil.getString(R.string.setting_currency_desc));
        selectMenuFragment.setSelectMenuList(selectMenuList);
        activity.showDialogFragment(selectMenuFragment);
        selectMenuFragment.setResultListener(new SelectMenuFragment.OnDataSelectBackListener() {
            @Override
            public void onSelect(SelectMenu selectMenu) {
                /*UnitUtils.setSettingMoneyType(selectMenu.value);
                currencyLayout.setmRightText(selectMenu.value);*/
                if (currencyCallback != null) {
                    currencyCallback.onCurrencySelect(selectMenu.value);
                }
            }
        });
    }


    public String priceFormatText(String priceType, String currency, String price) {
        StringBuilder currencyBuilder = new StringBuilder();

        if (ServiceInfo.PAYWAY_FREE.equals(priceType)) {
            currencyBuilder.append(ResourceUtil.getString(R.string.price_attribute_type_value_free));
            return currencyBuilder.toString();
        }

        int priceValue = (int)NumberUtils.parseFloat(price);

        if (priceValue < 0) {
            return currencyBuilder.toString();
        } else if (priceValue == 0) {
            currencyBuilder.append("0.00");
            return currencyBuilder.toString();
        }

        if (!TextUtils.isEmpty(currency)) {
            currencyBuilder.append(currency.toUpperCase());
            currencyBuilder.append(" ");
        }

        String formatPrice = NumberUtils.getMoneyFormat(price);

        if (ServiceInfo.PAYWAY_PER.equals(priceType)) {
            String perText = ResourceUtil.getString(R.string.trip_attribute_price_value_per_person, formatPrice);
            currencyBuilder.append(perText);
        } else {
            currencyBuilder.append(formatPrice);
        }

        return currencyBuilder.toString();
    }

    /**
     * 费用包含红色
     *
     * @param priceType
     * @param currency
     * @param price
     * @return
     */
    public SpannableStringBuilder priceFormatTextStyled(String priceType, String currency, String price) {
//        StringBuilder currencyBuilder = new StringBuilder();

        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();

        if (ServiceInfo.PAYWAY_FREE.equals(priceType)) {
            spannableStringBuilder.append(ResourceUtil.getString(R.string.price_attribute_type_value_free));
            return spannableStringBuilder;
        }

        String str = NumberUtils.getMoneyFormat(price);

        if (!TextUtils.isEmpty(currency)) {
            SpannableString currencySpan = new SpannableString(currency.toUpperCase() + " ");
            currencySpan.setSpan(new ForegroundColorSpan(ResourceUtil.getThemeColor()), 0, currencySpan.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            spannableStringBuilder.append(currencySpan);
        }

        spannableStringBuilder.append(str);

        return spannableStringBuilder;
    }

    public SpannableStringBuilder priceStyledFormatText(String currency, String price) {
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
        String str = NumberUtils.getMoneyFormat(price);

        if (!TextUtils.isEmpty(currency)) {
            SpannableString currencySpan = new SpannableString(currency.toUpperCase() + " ");
            currencySpan.setSpan(new ForegroundColorSpan(ResourceUtil.getThemeColor()), 0, currencySpan.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            spannableStringBuilder.append(currencySpan);
        }

        spannableStringBuilder.append(str);

        return spannableStringBuilder;
    }


}
